package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper,AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult list(SensitiveDto dto) {
        //检查请求参数
        if(dto==null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //创建分页
        Page<AdSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = Wrappers.lambdaQuery();
        //判断是否有查询条件
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        //封装条件查询
        IPage<AdSensitive> pageResult = page(page, lambdaQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) pageResult.getTotal());

        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //参数检查
        if(adSensitive==null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否有敏感词名称
        if(StringUtils.isBlank(adSensitive.getSensitives())){
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //封装创建时间
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //参数检查
        if(adSensitive==null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(adSensitive.getId()==null){
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断id是否存在数据
        AdSensitive one = getOne(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getId,adSensitive.getId()));
        if(one==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //执行更新
        updateById(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        if(id==null){
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        AdSensitive one = getOne(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getId, id));
        if(one==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        removeById(id);
        return ResponseResult.okResult();
    }
}
